package cn.icanci.aspect;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

import org.simpleframework.aop.annotation.Aspect;
import org.simpleframework.aop.annotation.Order;
import org.simpleframework.aop.aspect.DefaultAspect;

/**
 * @author icanci
 * @date Created in 2021/03/03 23:18
 * @version 1.0
 */
@Slf4j
@Aspect(pointcut = "within(org.simpleframework.aop.annotation.*)")
@Order(1)
public class ServiceTimeCalculatorAspect extends DefaultAspect {
    private long timestampCache;

    @Override
    public void before(Class<?> targetClass, Method method, Object[] args) throws Throwable {
        log.info("开始计时，执行的类是[{}],执行的方法是[{}],参数是[{}]", targetClass.getName(), method.getName(), args);
        timestampCache = System.currentTimeMillis();
    }

    @Override
    public Object afterReturning(Class<?> targetClass, Method method, Object[] args, Object returnObject) throws Throwable {
        long endTime = System.currentTimeMillis();
        long costTime = endTime - timestampCache;
        log.info("结束计时，执行的类是[{}],执行的方法是[{}],参数是[{}],返回值是[{}],时间是[{}]ms", targetClass.getName(), method.getName(), args, returnObject, costTime);
        return returnObject;
    }
}
